#include <spdlog/spdlog.h>
#include "GameServer.h"
#include "services/LoginService.h"
#include "services/LogoutService.h"
#include "services/MoveService.h"
#include "services/ChooseRoleService.h"
#include "services/CreateRoleService.h"
#include "spdlog/sinks/rotating_file_sink.h"


void StopHandler(int user1){
    spdlog::info("server stop normally");
    spdlog::get("rotate_logger")->flush();
    exit(0);
}

int main(int argc, char* argv[]) {
    if(argc<6) return -1;
    std::string dbhostname(argv[1]);
    std::string dbUser(argv[2]);
    std::string dbPW(argv[3]);
    std::string serverHost(argv[4]);
    int port = std::stoi(argv[5]);
    auto max_size = 1048576 * 5;
    auto max_files = 3;
    auto logger = spdlog::rotating_logger_mt("rotate_logger", "logs/rotating.txt", max_size, max_files);
    spdlog::set_default_logger(logger);
    spdlog::flush_every(std::chrono::seconds(3));

    signal(10,StopHandler);
    GameServer gameServer;
    gameServer.Handle(GameServer::ProtoType::loginReq,new LoginService);
    gameServer.Handle(GameServer::ProtoType::logoutReq,new LogoutService);
    gameServer.Handle(GameServer::ProtoType::moveReq,new MoveService);
    gameServer.Handle(GameServer::ProtoType::chooseRoleReq,new ChooseRoleService);
    gameServer.Handle(GameServer::ProtoType::createRoleReq,new CreateRoleService);
    gameServer.SetDatabase("tcp://"+dbhostname,dbUser,dbPW);
    gameServer.Start(serverHost,port);
    spdlog::warn("server stop!");
  return 0;
}
